package com.xiaoze.steampy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoze.steampy.common.lang.Result;
import com.xiaoze.steampy.entity.Game;

import com.xiaoze.steampy.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小泽
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/game")
public class GameController {

    @Autowired
    GameService gameService;

    /**
     * 查询所有game列表
     */

    @RequestMapping("/getGameAll")
    public Result  getAllGame(){
        List<Game> list = gameService.list();
         Map<String, List<Game>> map = new HashMap<>();
         map.put("Game",list);
        return Result.succ(map);
    }

    /**
     * 根据id获取game信息
     */
    @RequestMapping("/getGameById")
    public Result getGameById(@RequestParam("id") Integer id ){
        Game game = gameService.getById(id);
        Map<String, Game> map = new HashMap<>();
        map.put("gamemessage",game);
        return Result.succ(map);
    }

    /**
     * 根据用户id获取全部的game信息
     */
    @RequestMapping("/getgamelist")
    public Result getGameList(@RequestParam("username") String username){
        List<Game> games = gameService.getgameList(Integer.parseInt(username));
        HashMap<String, List<Game>> map = new HashMap<>();
        map.put("GameList",games);
        return Result.succ(map);
    }

    /*
    * 分页获取game信息
    * */

    @RequestMapping("/Pageall")
    public Result getall(Integer currentPage){

        if(currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        Page<Game> page = new Page<>(currentPage,5);
        IPage<Game> page1 = gameService.page(page, new QueryWrapper<Game>());

        return Result.succ(page1);
    }

    /*
     * 搜索
     * */

    @GetMapping("/search")
    public Result search(Integer currentPage,String search){

        if(currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        Page<Game> page = new Page<>(currentPage,5);
        IPage<Game> page1 = gameService.page(page, new QueryWrapper<Game>().like("name","%"+search+"%"));
        return  Result.succ(page1);
    }

    /*
    * 添加游戏
    * */

    @PostMapping("/addgame")
    public Result addgame(@Validated @RequestBody Game game){
         gameService.save(game);
        return Result.succ("返回成功");
    }

    /*
    * 根据id删除游戏
    * */

    @GetMapping("/deleteById")
    public Result deletegame(String id){
        gameService.removeById(Integer.parseInt(id));
        return Result.succ("删除成功");
    }
}
